package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;
import com.weipu.common.util.Logger;

public class CouponActiveModel implements Serializable {

	/**
	 * 唯一标志
	 */
	private static final long serialVersionUID = 2087228961726099587L;

	// 是否针对全场折扣 true针对全场
	private boolean IsWholeSite;
	// 当前货币
	private String CurrencyCode;
	// 当前货币符号
	private String CurrencyCodeStr;
	// 当前选择的品类下的SKU总行数。如果CurrencyCategoryId=0，就是该Coupon没有品类，只有SKU
	private int TotalCount;
	// 当前品类ID 如果CurrencyCategoryId=0，就是该Coupon没有品类
	private int CurrencyCategoryId;
	// coupon活动对应的品类信息可能为空
	private List<CatelogModel> Categories;
	// 产品信息 如果CurrencyCategoryId不为0默认返回第一条品类的产品数据
	private List<ProductModel> Products;
	// 当前Coupon信息
	private CouponModel Coupon;
	// 是否是MVP标记
	private boolean IsMvp;

	public boolean isIsMvp() {
		return IsMvp;
	}

	public void setIsMvp(boolean isMvp) {
		IsMvp = isMvp;
	}

	public boolean isIsWholeSite() {
		return IsWholeSite;
	}

	public void setIsWholeSite(boolean isWholeSite) {
		IsWholeSite = isWholeSite;
	}

	public String getCurrencyCode() {
		return CurrencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		CurrencyCode = currencyCode;
	}

	public String getCurrencyCodeStr() {
		return CurrencyCodeStr;
	}

	public void setCurrencyCodeStr(String currencyCodeStr) {
		CurrencyCodeStr = currencyCodeStr;
	}

	public int getTotalCount() {
		return TotalCount;
	}

	public void setTotalCount(int totalCount) {
		TotalCount = totalCount;
	}

	public int getCurrencyCategoryId() {
		return CurrencyCategoryId;
	}

	public void setCurrencyCategoryId(int currencyCategoryId) {
		CurrencyCategoryId = currencyCategoryId;
	}

	public List<CatelogModel> getCategories() {
		return Categories;
	}

	public void setCategories(List<CatelogModel> categories) {
		Categories = categories;
	}

	public List<ProductModel> getProducts() {
		return Products;
	}

	public void setProducts(List<ProductModel> products) {
		Products = products;
	}

	public CouponModel getCoupon() {
		return Coupon;
	}

	public void setCoupon(CouponModel coupon) {
		Coupon = coupon;
	}

	public static CouponActiveModel jsonToCouponActiveModel(
			JSONObject jsonObject) throws JSONException {
		CouponActiveModel couponActiveModel = new CouponActiveModel();

		couponActiveModel.setIsWholeSite(jsonObject
				.optBoolean(ServerConstant.CouponActive.IS_WHOLE_SITE));
		if (jsonObject.optString(jsonObject
				.optString(ServerConstant.CouponActive.CURRENCY_CODE)) != JSONObject.NULL
				&& !jsonObject
						.isNull(ServerConstant.CouponActive.CURRENCY_CODE)) {
			couponActiveModel.setCurrencyCode(jsonObject
					.optString(ServerConstant.CouponActive.CURRENCY_CODE));
		}

		if (jsonObject.optString(jsonObject
				.optString(ServerConstant.CouponActive.CURRENCY_CODE_STR)) != JSONObject.NULL
				&& !jsonObject
						.isNull(ServerConstant.CouponActive.CURRENCY_CODE_STR)) {
			couponActiveModel.setCurrencyCodeStr(jsonObject
					.optString(ServerConstant.CouponActive.CURRENCY_CODE_STR));
		}

		couponActiveModel.setCurrencyCategoryId(jsonObject
				.optInt(ServerConstant.CouponActive.CURRENCY_CATEGORYID));
		// Categories
		String CategoriesObj = jsonObject
				.getString(ServerConstant.CouponActive.CATEGORIES);
		if (null != CategoriesObj && !"null".equals(CategoriesObj)) {
			JSONArray jsArray = jsonObject
					.getJSONArray(ServerConstant.CouponActive.CATEGORIES);
			List<CatelogModel> catelogModels = new ArrayList<CatelogModel>();
			int size = jsArray.length();
			for (int i = 0; i < size; i++) {
				CatelogModel cateModel = new CatelogModel();
				JSONObject categoryJson = jsArray.getJSONObject(i);
				cateModel.setBrowseID(categoryJson
						.optInt(ServerConstant.Catelog.CONSTANT_ID));
				if (categoryJson
						.optString(ServerConstant.Catelog.CONSTANT_NEWHEADLINE) != JSONObject.NULL
						&& !categoryJson
								.isNull(ServerConstant.Catelog.CONSTANT_NEWHEADLINE)) {
					String name = categoryJson
							.optString(ServerConstant.Catelog.CONSTANT_NEWHEADLINE);
					String shortName = getShortName(name);
					cateModel.setShortName(shortName);
				}
				catelogModels.add(cateModel);
			}
			couponActiveModel.setCategories(catelogModels);
		}
		//

		// Products
		String productsObj = jsonObject
				.getString(ServerConstant.CouponActive.PRODUCTS);
		if (null != productsObj && !"null".equals(productsObj)) {
			JSONArray productArray = jsonObject
					.getJSONArray(ServerConstant.CouponActive.PRODUCTS);
			List<ProductModel> productModels = new ArrayList<ProductModel>();
			for (int i = 0; i < productArray.length(); i++) {
				ProductModel proModel = new ProductModel();
				JSONObject productJson = productArray.getJSONObject(i);
				proModel = ProductModel.json2product(productJson);
				productModels.add(proModel);
			}
			couponActiveModel.setProducts(productModels);

		}
		
		//isMVP 
		couponActiveModel.setIsMvp(jsonObject.optBoolean(ServerConstant.CouponActive.ISMVP));
		
		// Coupon
		// 目前Coupon数据没用到 暂不解析
		return couponActiveModel;
	}

	private static String getShortName(String name) {
		if(null==name){
			return "";
		}
		String newname=name.trim();
		if (newname.contains(" ")) {
			int spaceIndex = newname.indexOf(" ");
			String news = newname.substring(0, spaceIndex) + "\n"
					+ newname.substring(spaceIndex + 1, newname.length());
			return news;
		}
		return newname;
	}

	public static CouponActiveModel jsonToCouponActiveModelTest(
			JSONObject jsonObject) {
		CouponActiveModel couponActiveModel = new CouponActiveModel();

		couponActiveModel.setIsWholeSite(false);

		couponActiveModel.setCurrencyCategoryId(199);
		// Categories
		List<CatelogModel> catelogModels = new ArrayList<CatelogModel>();
		int size = 8;
		for (int i = 0; i < size; i++) {
			CatelogModel cateModel = new CatelogModel();
			cateModel.setBrowseID(199);
			// String
			// name=categoryJson.optString(ServerConstant.Catelog.CONSTANT_NEWHEADLINE);
			//
			String name = "AAAAA";
			if (size == 1) {
				// if(name.contains(" ")){
				//
				// }
			}
			cateModel.setShortName(name);
			catelogModels.add(cateModel);
		}
		couponActiveModel.setCategories(catelogModels);

		// Products
		List<ProductModel> productModels = new ArrayList<ProductModel>();
		for (int i = 0; i < 5; i++) {
			ProductModel proModel = new ProductModel();
			proModel.setBigPictureURL(new String[] { "http://img.dxcdn.com/productimages/sku_139290_1_small.jpg" });
			proModel.setMediumPictureURL(new String[] { "http://img.dxcdn.com/productimages/sku_139290_1_small.jpg" });
			proModel.setSmallPictureURL(new String[] { "http://img.dxcdn.com/productimages/sku_139290_1_small.jpg" });
			proModel.setListprice(11.27f);
			proModel.setPrice(11.27f);
			proModel.setSku(10086);
			proModel.setShortHeadLine1("wo shi ce shi de sku");
			proModel.setReviewCount(12);
			proModel.setProductRating(5.0f);
			proModel.setFreeShipping(6);
			proModel.setWebUrl("http://www.163.com");
			productModels.add(proModel);
		}
		couponActiveModel.setProducts(productModels);

		// Coupon
		// 目前Coupon数据没用到 暂不解析
		return couponActiveModel;
	}

}
